package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanmait.employment.entity.*;
import com.wanmait.employment.mapper.*;
import com.wanmait.employment.service.RecruitmentDeliveryService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class RecruitmentDeliveryServiceImpl implements RecruitmentDeliveryService {
    @Resource
    private RecruitmentDeliveryMapper recruitmentDeliveryMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private RecruitmentEducationMapper educationMapper;
    @Resource
    private WorkExperienceMapper workExperienceMapper;

    @Resource
    private StateMapper stateMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private ExpectedSalaryMapper expectedSalaryMapper;
//    @Resource
//    private EntryTimeMapper entryTimeMapper;
    @Resource
    private NatureWorkMapper natureWorkMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private RecruitmentPostMapper recruitmentPostMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<RecruitmentDelivery> selectRecruitmentDelivery(Integer publisherId) {
        QueryWrapper<RecruitmentDelivery> recruitmentDeliveryQueryWrapper = new QueryWrapper<>();
        recruitmentDeliveryQueryWrapper.inSql("recruitment_post_id","select publisher_id from recruitment_post where publisher_id = "+publisherId);
        recruitmentDeliveryQueryWrapper.orderByDesc("delivery_time");
        List<RecruitmentDelivery> recruitmentDeliveries = recruitmentDeliveryMapper.selectList(recruitmentDeliveryQueryWrapper);
        recruitmentDeliveries.forEach(recruitmentDelivery -> {
            LambdaQueryWrapper<RecruitmentPost> recruitmentPostLambdaQueryWrapper=new LambdaQueryWrapper<>();
            recruitmentPostLambdaQueryWrapper.eq(RecruitmentPost::getId,recruitmentDelivery.getRecruitmentPostId());
            RecruitmentPost recruitmentPost=recruitmentPostMapper.selectOne(recruitmentPostLambdaQueryWrapper);
            recruitmentDelivery.setRecruitmentPost(recruitmentPost);

            LambdaQueryWrapper<Userinfo> userinfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userinfoLambdaQueryWrapper.eq(Userinfo::getId, recruitmentDelivery.getUserinfoId());
            Userinfo userinfo = userinfoMapper.selectOne(userinfoLambdaQueryWrapper);

            List<Nation> nations = (List<Nation>) redisTemplate.opsForValue().get("employment:nation");
            nations.forEach(nation -> {
                if (nation.getId().equals(userinfo.getNationId())) {
                    userinfo.setNation(nation);
                    recruitmentDelivery.setUserinfo(userinfo);
                }
            });
//            List<MaritalStatus> maritalStatuses = (List<MaritalStatus>) redisTemplate.opsForValue().get("maritalStatus");
//            maritalStatuses.forEach(maritalStatuses1 -> {
//                if (maritalStatuses1.getId().equals(userinfo.getMaritalStatusId())) {
//                    userinfo.setMaritalStatus(maritalStatuses1);
//                    recruitmentDelivery.setUserinfo(userinfo);
//                }
//            });

//            List<RecruitmentEducation> recruitmentEducations = (List<RecruitmentEducation>) redisTemplate.opsForValue().get("employment:recruitmentEducation");
//            recruitmentEducations.forEach(recruitmentEducations1 -> {
//                if (recruitmentEducations1.getId().equals(userinfo.getRecruitmentId())) {
//                    userinfo.setRecruitmentEducation(recruitmentEducations1);
//                    recruitmentDelivery.setUserinfo(userinfo);
//                }
//            });
//
//            List<WorkExperience> workExperiences = (List<WorkExperience>) redisTemplate.opsForValue().get("employment:workExperiences");
//            workExperiences.forEach(workExperiences1 -> {
//                if (workExperiences1.getId().equals(userinfo.getWorkexperienceId())) {
//                    userinfo.setWorkExperience(workExperiences1);
//                    recruitmentDelivery.setUserinfo(userinfo);
//                }
//            });

            //求职状态联表查询
            LambdaQueryWrapper<State> stateLambdaQueryWrapper=new LambdaQueryWrapper<>();
            stateLambdaQueryWrapper.eq(State::getId,userinfo.getStateId());
            userinfo.setState(stateMapper.selectOne(stateLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
            //期望职位
            LambdaQueryWrapper<Position> positionLambdaQueryWrapper=new LambdaQueryWrapper<>();
            positionLambdaQueryWrapper.eq(Position::getId,userinfo.getPositionId());
            userinfo.setPosition(positionMapper.selectOne(positionLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
            //期望薪资
            LambdaQueryWrapper<ExpectedSalary> expectedSalaryLambdaQueryWrapper=new LambdaQueryWrapper<>();
            expectedSalaryLambdaQueryWrapper.eq(ExpectedSalary::getId,userinfo.getPositionId());
            userinfo.setExpectedSalary(expectedSalaryMapper.selectOne(expectedSalaryLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
//            //将要入职时间
//            LambdaQueryWrapper<EntryTime> entryTimeLambdaQueryWrapper=new LambdaQueryWrapper<>();
//            entryTimeLambdaQueryWrapper.eq(EntryTime::getId,userinfo.getEntryTimeId());
//            userinfo.setEntryTime(entryTimeMapper.selectOne(entryTimeLambdaQueryWrapper));
//            recruitmentDelivery.setUserinfo(userinfo);
            //求职类型表
            LambdaQueryWrapper<NatureWork> natureWorkLambdaQueryWrapper=new LambdaQueryWrapper<>();
            natureWorkLambdaQueryWrapper.eq(NatureWork::getId,userinfo.getNatureWork());
            userinfo.setNatureWork(natureWorkMapper.selectOne(natureWorkLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);

            //查询区
            LambdaQueryWrapper<Address> addressLambdaQueryWrapper=new LambdaQueryWrapper<>();
            addressLambdaQueryWrapper.eq(Address::getId,userinfo.getAddressId());
            Address address=addressMapper.selectOne(addressLambdaQueryWrapper);
            userinfo.setAddress(address);
            //查询市
            LambdaQueryWrapper<Address> cityLambadaQueryWrapper=new LambdaQueryWrapper<>();
            cityLambadaQueryWrapper.eq(Address::getId,address.getParentId());
            Address city=addressMapper.selectOne(cityLambadaQueryWrapper);
            userinfo.setCity(city);
            //查询省
            LambdaQueryWrapper<Address> provinceQueryWrapper=new LambdaQueryWrapper<>();
            provinceQueryWrapper.eq(Address::getId,city.getParentId());
            Address province=addressMapper.selectOne(provinceQueryWrapper);
            userinfo.setProvince(province);
        });
        return recruitmentDeliveries;
    }


    @Override
    public List<RecruitmentDelivery> findList(Integer publisherId) {
        QueryWrapper<RecruitmentDelivery> recruitmentDeliveryQueryWrapper = new QueryWrapper<>();
        recruitmentDeliveryQueryWrapper.inSql("recruitment_post_id","select publisher_id from recruitment_post where publisher_id = "+publisherId);
        recruitmentDeliveryQueryWrapper.orderByDesc("delivery_time");
        List<RecruitmentDelivery> recruitmentDeliveries = recruitmentDeliveryMapper.selectList(recruitmentDeliveryQueryWrapper);
        recruitmentDeliveries.forEach(recruitmentDelivery -> {
            LambdaQueryWrapper<RecruitmentPost> recruitmentPostLambdaQueryWrapper=new LambdaQueryWrapper<>();
            recruitmentPostLambdaQueryWrapper.eq(RecruitmentPost::getId,recruitmentDelivery.getRecruitmentPostId());
            RecruitmentPost recruitmentPost=recruitmentPostMapper.selectOne(recruitmentPostLambdaQueryWrapper);
            recruitmentDelivery.setRecruitmentPost(recruitmentPost);
            //查找个人简历
            LambdaQueryWrapper<Userinfo> userinfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userinfoLambdaQueryWrapper.eq(Userinfo::getId, recruitmentDelivery.getUserinfoId());
            Userinfo userinfo = userinfoMapper.selectOne(userinfoLambdaQueryWrapper);
            //联动学历
            LambdaQueryWrapper<RecruitmentEducation> educationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            educationLambdaQueryWrapper.eq(RecruitmentEducation::getId, userinfo.getRecruitmentId());
            RecruitmentEducation recruitmentEducation = educationMapper.selectOne(educationLambdaQueryWrapper);
            userinfo.setRecruitmentEducation(recruitmentEducation);
            recruitmentDelivery.setUserinfo(userinfo);
            //联动工作经验
            LambdaQueryWrapper<WorkExperience> workExperienceLambdaQueryWrapper=new LambdaQueryWrapper<>();
            workExperienceLambdaQueryWrapper.eq(WorkExperience::getId,userinfo.getWorkexperienceId());
            userinfo.setWorkExperience( workExperienceMapper.selectOne(workExperienceLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
            //联表民族
//            LambdaQueryWrapper<Nation> nationLambdaQueryWrapper=new LambdaQueryWrapper<>();
//            nationLambdaQueryWrapper.eq(Nation::getId,userinfo.getNationId());
//            userinfo.setNation(nationMapper.selectOne(nationLambdaQueryWrapper));
//            recruitmentDelivery.setUserinfo(userinfo);
//            //联表婚姻状态
//            LambdaQueryWrapper<MaritalStatus> maritalStatusLambdaQueryWrapper=new LambdaQueryWrapper<>();
//            maritalStatusLambdaQueryWrapper.eq(MaritalStatus::getId,userinfo.getMaritalStatusId());
//            userinfo.setMaritalStatus(maritalStatusMapper.selectOne(maritalStatusLambdaQueryWrapper));
//            recruitmentDelivery.setUserinfo(userinfo);
            //求职状态联表查询
            LambdaQueryWrapper<State> stateLambdaQueryWrapper=new LambdaQueryWrapper<>();
            stateLambdaQueryWrapper.eq(State::getId,userinfo.getStateId());
            userinfo.setState(stateMapper.selectOne(stateLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
            //期望职位
            LambdaQueryWrapper<Position> positionLambdaQueryWrapper=new LambdaQueryWrapper<>();
            positionLambdaQueryWrapper.eq(Position::getId,userinfo.getPositionId());
            userinfo.setPosition(positionMapper.selectOne(positionLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
            //期望薪资
            LambdaQueryWrapper<ExpectedSalary> expectedSalaryLambdaQueryWrapper=new LambdaQueryWrapper<>();
            expectedSalaryLambdaQueryWrapper.eq(ExpectedSalary::getId,userinfo.getPositionId());
            userinfo.setExpectedSalary(expectedSalaryMapper.selectOne(expectedSalaryLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
//            //将要入职时间
//            LambdaQueryWrapper<EntryTime> entryTimeLambdaQueryWrapper=new LambdaQueryWrapper<>();
//            entryTimeLambdaQueryWrapper.eq(EntryTime::getId,userinfo.getEntryTimeId());
//            userinfo.setEntryTime(entryTimeMapper.selectOne(entryTimeLambdaQueryWrapper));
//            recruitmentDelivery.setUserinfo(userinfo);
            //求职类型表
            LambdaQueryWrapper<NatureWork> natureWorkLambdaQueryWrapper=new LambdaQueryWrapper<>();
            natureWorkLambdaQueryWrapper.eq(NatureWork::getId,userinfo.getNatureWorkId());
            userinfo.setNatureWork(natureWorkMapper.selectOne(natureWorkLambdaQueryWrapper));
            recruitmentDelivery.setUserinfo(userinfo);
            //查询区
            LambdaQueryWrapper<Address> addressLambdaQueryWrapper=new LambdaQueryWrapper<>();
            addressLambdaQueryWrapper.eq(Address::getId,userinfo.getAddressId());
            Address address=addressMapper.selectOne(addressLambdaQueryWrapper);
            userinfo.setAddress(address);
            //查询市
            LambdaQueryWrapper<Address> cityLambadaQueryWrapper=new LambdaQueryWrapper<>();
            cityLambadaQueryWrapper.eq(Address::getId,address.getParentId());
            Address city=addressMapper.selectOne(cityLambadaQueryWrapper);
            userinfo.setCity(city);
//            //查询省
//            LambdaQueryWrapper<Address> provinceQueryWrapper=new LambdaQueryWrapper<>();
//            provinceQueryWrapper.eq(Address::getId,city.getParentId());
//            Address province=addressMapper.selectOne(provinceQueryWrapper);
//            userinfo.setProvince(province);
        });
        int limit = Math.min(recruitmentDeliveries.size(), 4); // 确定最终选取的元素数量
        recruitmentDeliveries = recruitmentDeliveries.subList(0, limit); // 截取前 limit 个元素
        return recruitmentDeliveries;
    }
}
